package org.example.big_data.controller;

import org.example.big_data.pojo.Category;
import org.example.big_data.pojo.Result;
import org.example.big_data.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    //1.添加文章分类接口,前端传入json，参与查询的是整个对象category，在service层补充缺少i的属性
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category){
        categoryService.add(category);
        return Result.success("好嘞！");
    }

    //2.GET请求：/category获取所有文章分类列表，响应数据Result<List<Category>>中data中为一个列表，列表中每个元素为一个Category对象
    @GetMapping
    public Result<List<Category>> list(){
        List<Category> list = categoryService.list();
        return Result.success(list);
    }

    //3./category/detail根据ID获取文章分类详情，即根据id查询出一整行数据，转化为Category对象
    @GetMapping("/detail")
    public Result<Category> detail(Integer id){
        Category category = categoryService.detail(id);
        return Result.success(category);
    }

    //4.  PUT方式请求/category：更新文章分类，**请求中携带json数据为POJO对象的部分属性，controller中@RequestBody转化**
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category){
        categoryService.update(category);
        return Result.success("更改成功");
    }

    //5. DELETE方式请求/category：根据id删除文章分类，尝试使用RESTful风格的请求
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        categoryService.delete(id);
        return Result.success("删除成功");
    }


}
